package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.common.exception.LeaseException;
import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.admin.mapper.*;
import com.atguigu.lease.web.admin.service.*;
import com.atguigu.lease.web.admin.vo.attr.AttrValueVo;
import com.atguigu.lease.web.admin.vo.graph.GraphVo;
import com.atguigu.lease.web.admin.vo.room.RoomDetailVo;
import com.atguigu.lease.web.admin.vo.room.RoomItemVo;
import com.atguigu.lease.web.admin.vo.room.RoomQueryVo;
import com.atguigu.lease.web.admin.vo.room.RoomSubmitVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

import static com.fasterxml.jackson.databind.type.LogicalType.Collection;

/**
 * @author liubo
 * @description 针对表【room_info(房间信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class RoomInfoServiceImpl extends ServiceImpl<RoomInfoMapper, RoomInfo>
        implements RoomInfoService {
    @Autowired
    private RoomInfoMapper roomInfoMapper;
    @Autowired
    private ApartmentInfoMapper apartmentInfoMapper;
    @Autowired
    private GraphInfoMapper graphInfoMapper;
    @Autowired
    private AttrValueMapper attrValueMapper;
    @Autowired
    private FacilityInfoMapper facilityInfoMapper;
    @Autowired
    private PaymentTypeMapper paymentTypeMapper;
    @Autowired
    private LeaseTermMapper leaseTermMapper;
    @Autowired
    private LabelInfoMapper labelInfoMapper;
    @Autowired
    private RoomFacilityService roomFacilityService;
    @Autowired
    private RoomLabelService roomLabelService;
    @Autowired
    private RoomAttrValueService roomAttrValueService;
    @Autowired
    private RoomPaymentTypeService roomPaymentTypeService;
    @Autowired
    private RoomLeaseTermService roomLeaseTermService;
    @Autowired
    private GraphInfoService graphInfoService;
    @Autowired
    private LeaseAgreementMapper leaseAgreementMapper;

    @Override
    public void saveOrUpdateInfo(RoomSubmitVo roomSubmitVo) {
        Long id = roomSubmitVo.getId();
        //不管是新增还是更新，房间的基本信息都需要保存
        this.saveOrUpdate(roomSubmitVo);

        //判断是否是新增还是更新
        if (id != null) {
            //修改
            //删除房间的图信息
            roomFacilityService.remove(new LambdaQueryWrapper<RoomFacility>().eq(RoomFacility::getRoomId, roomSubmitVo.getId()));//删除房间的配套信息
            roomLabelService.remove(new LambdaQueryWrapper<RoomLabel>().eq(RoomLabel::getLabelId, roomSubmitVo.getId()));//删除房间的标签信息
            roomPaymentTypeService.remove(new LambdaQueryWrapper<RoomPaymentType>().eq(RoomPaymentType::getRoomId, roomSubmitVo.getId()));//删除房间的支付类型信息
            roomLeaseTermService.remove(new LambdaQueryWrapper<RoomLeaseTerm>().eq(RoomLeaseTerm::getRoomId, roomSubmitVo.getId()));//删除房间的租赁期限信息
            roomAttrValueService.remove(new LambdaQueryWrapper<RoomAttrValue>().eq(RoomAttrValue::getRoomId, roomSubmitVo.getId()));//删除房间的属性值信息
            graphInfoService.remove(new LambdaQueryWrapper<GraphInfo>().eq(GraphInfo::getItemId, roomSubmitVo.getId()).eq(GraphInfo::getItemType, ItemType.ROOM));//删除房间的图信息
        }
        //再添加房价相关的配套信息，标签信息，支付类型信息，租赁期限信息，属性值信息
        //添加配套信息
        List<Long> facilityInfoIds = roomSubmitVo.getFacilityInfoIds();//获取前端传过来的所有的房间的配套信息id
        if (CollectionUtils.isEmpty(facilityInfoIds)) {
            List<RoomFacility> facilityInfoList = new ArrayList<>();
            for (Long facilityInfoId : facilityInfoIds) {
                RoomFacility roomFacility = RoomFacility.builder().facilityId(facilityInfoId).roomId(roomSubmitVo.getId()).build();
                facilityInfoList.add(roomFacility);
            }
            roomFacilityService.saveBatch(facilityInfoList);//批量保存房间的配套信息
        }


        //添加房间标签信息

        List<Long> labelInfoIds = roomSubmitVo.getLabelInfoIds();
        if (CollectionUtils.isEmpty(labelInfoIds)) {
            List<RoomLabel> labelInfoList = new ArrayList<>();
            for (Long labelInfoId : labelInfoIds) {
                RoomLabel roomLabel = RoomLabel.builder().labelId(labelInfoId).roomId(roomSubmitVo.getId()).build();
                labelInfoList.add(roomLabel);
            }
            roomLabelService.saveBatch(labelInfoList);
        }


        //添加房间的支付类型信息
        List<Long> paymentTypeIds = roomSubmitVo.getPaymentTypeIds();
        if (CollectionUtils.isEmpty(paymentTypeIds)) {
            List<RoomPaymentType> paymentTypeInfoList = new ArrayList<>();
            for (Long paymentTypeId : paymentTypeIds) {
                RoomPaymentType roomPaymentType = RoomPaymentType.builder().paymentTypeId(paymentTypeId).roomId(roomSubmitVo.getId()).build();
                paymentTypeInfoList.add(roomPaymentType);
            }
            roomPaymentTypeService.saveBatch(paymentTypeInfoList);
        }


        //添加房间的租赁期限信息
        List<Long> leaseTermIds = roomSubmitVo.getLeaseTermIds();
        if (CollectionUtils.isEmpty(leaseTermIds)) {
            List<RoomLeaseTerm> leaseTermInfoList = new ArrayList<>();
            for (Long leaseTermId : leaseTermIds) {
                RoomLeaseTerm roomLeaseTerm = RoomLeaseTerm.builder().leaseTermId(leaseTermId).roomId(roomSubmitVo.getId()).build();
                leaseTermInfoList.add(roomLeaseTerm);
            }
            roomLeaseTermService.saveBatch(leaseTermInfoList);
        }


        //添加房间的属性值信息
        List<Long> attrValueIds = roomSubmitVo.getAttrValueIds();
        if (CollectionUtils.isEmpty(attrValueIds)) {
            List<RoomAttrValue> attrValueInfoList = new ArrayList<>();
            for (Long attrValueId : attrValueIds) {
                RoomAttrValue roomAttrValue = RoomAttrValue.builder().attrValueId(attrValueId).roomId(roomSubmitVo.getId()).build();
                attrValueInfoList.add(roomAttrValue);
            }
            roomAttrValueService.saveBatch(attrValueInfoList);
        }
        //添加房间的图信息
        List<GraphVo> graphVoList = roomSubmitVo.getGraphVoList();
        if (CollectionUtils.isEmpty(graphVoList)) {
            List<GraphInfo> graphInfos = new ArrayList<>();
            for (GraphVo graphVo : graphVoList) {
                GraphInfo graphInfo = new GraphInfo();
                graphInfo.setUrl(graphVo.getUrl());
                graphInfo.setItemId(roomSubmitVo.getId());
                graphInfo.setItemType(ItemType.ROOM);
                graphInfo.setName(graphVo.getName());
                graphInfos.add(graphInfo);
            }
            graphInfoService.saveBatch(graphInfos);
        }

    }

    @Override
    public IPage<RoomItemVo> pageItem(Page<RoomItemVo> roomItemVoPage, RoomQueryVo queryVo) {
        return roomInfoMapper.pageItem(roomItemVoPage, queryVo);
    }

    @Override
    public RoomDetailVo getDetailById(Long id) {
        //查询到的房间以及房间相关的信息对象,这是从数据库往外出的，因此需要Vo对象封装
        RoomDetailVo roomDetailVo = new RoomDetailVo();

        //查询房间的基本信息
        RoomInfo roomInfo = this.getById(id);

        //房间所属的公寓
        ApartmentInfo apartmentInfo = apartmentInfoMapper.selectById(roomInfo.getApartmentId());
        //查询房间的图片
        List<GraphVo> graphVoList = graphInfoMapper.selectListByItemTypeAndItemId(id, ItemType.ROOM);
        //查询房间的属性信息
        List<AttrValueVo> attrValueVoList = attrValueMapper.selectListByRoomId(id);
        //查询房间的配套
        List<FacilityInfo> facilityInfoList = facilityInfoMapper.selectListByRoomId(id);
        //查询房间的标签
        List<LabelInfo> labelInfoList = labelInfoMapper.selectListByRoomId(id);
        //查询房间的支付方式
        List<PaymentType> paymentTypeList = paymentTypeMapper.selectListByRoomId(id);
        //查询房间的租期
        List<LeaseTerm> leaseTermList = leaseTermMapper.selectListByRoomId(id);

        BeanUtils.copyProperties(roomInfo, roomDetailVo);
        roomDetailVo.setApartmentInfo(apartmentInfo);
        roomDetailVo.setGraphVoList(graphVoList);
        roomDetailVo.setAttrValueVoList(attrValueVoList);
        roomDetailVo.setFacilityInfoList(facilityInfoList);
        roomDetailVo.setLabelInfoList(labelInfoList);
        roomDetailVo.setPaymentTypeList(paymentTypeList);
        roomDetailVo.setLeaseTermList(leaseTermList);


        return roomDetailVo;
    }

    @Override
    public void removeByIdInfo(Long id) {
        //先从租约管理中获取一个与该id相同的房间id的租约信息，再做判断
        LeaseAgreement leaseAgreement = leaseAgreementMapper.selectOne(new LambdaQueryWrapper<LeaseAgreement>().eq(LeaseAgreement::getRoomId, id).in(LeaseAgreement::getStatus, 2, 5));
        if(leaseAgreement!=null){ //房间已被出租
            throw  new LeaseException(555,"房间已被出租,暂时不能删除！！");//抛出一个异常，提醒前端以及客户，不能直接删除房间信息
            //抛出异常后面的就不走了
        }
        //删除房间的基本信息
        this.removeById(id);
        //删除房间关联表的信息
        //删除配套信息
        roomFacilityService.remove(new LambdaQueryWrapper<RoomFacility>().eq(RoomFacility::getRoomId, id));
        //删除房间的标签信息
        roomLabelService.remove(new LambdaQueryWrapper<RoomLabel>().eq(RoomLabel::getRoomId, id));
        //删除房间的支付类型信息
        roomPaymentTypeService.remove(new LambdaQueryWrapper<RoomPaymentType>().eq(RoomPaymentType::getRoomId, id));
        //删除房间的租赁期限信息
        roomLeaseTermService.remove(new LambdaQueryWrapper<RoomLeaseTerm>().eq(RoomLeaseTerm::getRoomId, id));
        //删除房间的属性值信息
        roomAttrValueService.remove(new LambdaQueryWrapper<RoomAttrValue>().eq(RoomAttrValue::getRoomId, id));
        //删除房间的图信息
        graphInfoService.remove(new LambdaQueryWrapper<GraphInfo>().eq(GraphInfo::getItemId, id).eq(GraphInfo::getItemType, ItemType.ROOM));

    }


}


